﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 废料到货管理 : System.Web.UI.Page
    {
        string sql =
@"SELECT t1.年度
,t1.月份
,t2.废料名称
,t2.规格
,t3.名称 as 供货或使用
,cast(abs(t1.数量) as float) as 数量
,[dbo].[废料季度计划单价](t1.废料ID, t1.年度,t1.月份) as 计划单价
,[dbo].[废料季度实际单价](t1.废料ID, t1.年度,t1.月份) as 实际单价
,[dbo].[废料季度计划单价](t1.废料ID, t1.年度,t1.月份)*t1.数量 as 计划金额
,[dbo].[废料季度实际单价](t1.废料ID, t1.年度,t1.月份)*t1.数量 as 实际金额
FROM 废料分配表 t1
left outer join 废料 t2 on t2.ID=t1.废料ID
left outer join 生产单位 t3 on t3.ID=t1.分厂ID
";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.LeiBie.DataBind();
                this.ShouHuoDanWei.DataBind();               
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = sql;
            }
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {         

            this.GridView1.SelectedIndex = -1;        

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {       
                // ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料发货登记.aspx?id=" + list + "',600,300);</script>");            
        }
        private string GetState()
        {
            string state = "where 0=0";

            if (this.NianDu.SelectedItem.Value != "0")
            {
                 state += " and t1.年度="+this.NianDu.Text;
            }
            if (this.YueFen.SelectedItem.Value != "0")
            {
                state += " and t1.月份=" + this.YueFen.SelectedItem.Value;
            }
            if (this.LeiBie.SelectedItem.Value != "0")
            {
                state += " and t1.废料ID=" + this.LeiBie.SelectedItem.Value;
            }

            if (this.ShouHuoDanWei.Text != "0")
            {
                state += " and t3.ID=" + this.ShouHuoDanWei.SelectedItem.Value;             
            }

         

            //if (state.StartsWith(" and"))
            //{
            //    state = state.Substring(4);
            //}
            //if (state != "")
            //{
            //    state = " where " + state;
            //}
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {

            string state = this.GetState();        
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = sql + state + " order by t1.年度 desc";
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.NianDu.Text = "0";
            this.YueFen.Text = "0";
            this.LeiBie.Text = "0";
            this.ShouHuoDanWei.Text = "0";
        }
       
    }
}
